package com.sinry.swagger.processors;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.sinry.api.client.feign.message.MessageClient;
import com.sinry.common.core.domain.dto.ApiRegisterDto;
import com.sinry.message.domain.MessageDto;
import com.sinry.message.enums.RabbitQueue;
import com.sinry.swagger.properties.SwaggerProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @author linxinlei
 * @project sinry-cloud
 * @description springboot启动后置处理器
 * @date 2022/5/22
 **/
@Slf4j
@Component
@ConditionalOnProperty(name = "sinry.swagger.report.enable", havingValue = "true")
public class SwaggerPostProcessor implements ApplicationRunner {

    private final SwaggerProperties swaggerProperties;
    private final SwaggerResourcesProvider swaggerResources;
    private final NacosDiscoveryProperties nacosDiscoveryProperties;
    private final MessageClient messageClient;

    @Autowired
    public SwaggerPostProcessor(SwaggerProperties swaggerProperties,
                                SwaggerResourcesProvider swaggerResources,
                                NacosDiscoveryProperties nacosDiscoveryProperties,
                                MessageClient messageClient
                                ) {
        this.swaggerProperties = swaggerProperties;
        this.swaggerResources = swaggerResources;
        this.nacosDiscoveryProperties = nacosDiscoveryProperties;
        this.messageClient = messageClient;
    }

    /**
     * 自动上报swagger信息给api聚合平台
     */
    @Override
    public void run(ApplicationArguments args) {
        List<SwaggerResource> swaggerResourcesList = swaggerResources.get();
        if (CollectionUtil.isEmpty(swaggerResourcesList)) {
            return;
        }
        for (SwaggerResource swaggerResource : swaggerResourcesList) {
            ApiRegisterDto apiRegisterDto = ApiRegisterDto.builder()
                    .name(swaggerProperties.getName())
                    .serviceName(nacosDiscoveryProperties.getService())
                    .servicePath("/" + nacosDiscoveryProperties.getService())
                    .location(swaggerResource.getUrl())
                    .groupName(nacosDiscoveryProperties.getGroup())
                    .basicAuthEnable(0)
                    .swaggerVersion(swaggerResource.getSwaggerVersion())
                    .build();
            CompletableFuture.runAsync(
                    () -> messageClient.send(RabbitQueue.SWAGGER_REPORT, MessageDto.pack(apiRegisterDto)));
        }
    }
}
